//
//  ST_TimedRewardDefaultView.swift
//  btvideo
//
//  Created by Jason on 2022/1/21.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
//import Rswift
import SwiftyJSON

/// 想要对UI 适配有对应的测试工程 在 /Users/jasoncute/Desktop/临时/lottieTest/lottieTest.xcworkspace

class ST_TimedRewardDefaultView: UIView {
    
    private var json:JSON
    
    init(rewardJson:JSON){
        
        self.json = rewardJson
        super.init(frame: .zero)
        
        
        let compatibleOffSet:CGFloat = iPhoneXSeries ? 0 : -70
        
        let backgroundImageViewRatio:CGFloat = 552.0 / 375.0
        
        self.addSubview(background_image_view)
        background_image_view.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalToSuperview().offset(29+compatibleOffSet)
            make.height.equalTo(background_image_view.snp.width).multipliedBy(backgroundImageViewRatio)
        }
        
        self.addSubview(spark_animate_view)
        spark_animate_view.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: compatibleOffSet, left: 0, bottom: iPhoneXSeries ? 0 : 70, right: 0))
        }
        
        self.addSubview(coin_animate_view1)
        coin_animate_view1.snp.makeConstraints { make in
            make.width.equalTo(TIANCompatibleValue(value: 999/3.0))
            make.height.equalTo(TIANCompatibleValue(value: 999/3.0))
            make.top.equalToSuperview().offset(509/3.0+compatibleOffSet)
            make.left.equalToSuperview().offset(-110)
        }
        
        self.addSubview(coin_animate_view2)
        coin_animate_view2.snp.makeConstraints { make in
            make.width.equalTo(TIANCompatibleValue(value: 999/3.0))
            make.height.equalTo(TIANCompatibleValue(value: 999/3.0))
            make.top.equalTo(background_image_view.snp.bottom).offset(TIANCompatibleValue(value: -175))
            make.right.equalToSuperview().offset(356/3.0)
        }
        
        
        self.addSubview(open_animate_view)
        open_animate_view.snp.makeConstraints { make in
            make.centerX.equalTo(background_image_view)
            make.centerY.equalTo(background_image_view).offset(25)
            make.width.height.equalTo(TIANCompatibleValue(value: 120))
        }
        
        self.addSubview(open_btn)
        open_btn.snp.makeConstraints { make in
            make.center.equalTo(open_animate_view)
            make.width.height.equalTo(60)
        }
        
        
        [spark_animate_view,coin_animate_view1,coin_animate_view2,open_animate_view].forEach{ $0.play()}
        
    }
    
    private func layout_opened_u_i(){
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(background_image_view).offset(TIANCompatibleValue(value: 248))
        }
        
        self.addSubview(rewardView)
        rewardView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(21)
            make.height.equalTo(67)
        }
        
        self.addSubview(receive_award_btn)
        receive_award_btn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(background_image_view.snp.bottom).offset(TIANCompatibleValue(value: -80))
            make.width.equalTo(ScreenWidth/2.0)
            make.height.equalTo(66)
        }
        
        
        rewardView.textValueLabel.text = json["coin"].stringValue
        rewardView.unitTextLabel.text = json["desc"].stringValue
        
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var background_image_view:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("timed_reward_background_img"))
        return v
    }()
    
    
    fileprivate lazy var spark_animate_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "timed_background_spark", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.loopAnimation = true
        
        control.isUserInteractionEnabled = false
        
        return control
    }()
    
    
    fileprivate lazy var coin_animate_view1:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "timed_background_coin2", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.loopAnimation = true
        
        control.isUserInteractionEnabled = false
        
        return control
    }()
    
    fileprivate lazy var coin_animate_view2:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "timed_background_coin1", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.loopAnimation = true
        
        control.isUserInteractionEnabled = false
        
        return control
    }()
    
    fileprivate lazy var open_animate_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "timed_open_handle_icon", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.loopAnimation = true
        
        control.isUserInteractionEnabled = false
        
        return control
    }()
    
    fileprivate lazy var open_btn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.addTarget(self, action: #selector(openHandle), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    fileprivate lazy var receive_award_btn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        //        btn.backgroundColor = UIColor(hex: 0x834394, transparency: 0.3)
        btn.addTarget(self, action: #selector(receive_award_handle), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.text = json["title"].stringValue
        l.textColor = .white
        l.font = .systemFont(ofSize: 16, weight: .regular)
        return l
    }()
    
    fileprivate lazy var rewardView:ST_TimedRewardValueView = {
        let v = ST_TimedRewardValueView()
        return v
    }()
    
}


extension ST_TimedRewardDefaultView{
    
    @objc fileprivate func openHandle(){
        
        background_image_view.image = ST_ImageUtils.imageName("timed_reward_opened_background_img")
        
        open_animate_view.stop()
        open_animate_view.removeFromSuperview()
        open_btn.removeFromSuperview()
        
        layout_opened_u_i()
        
    }
    
    @objc fileprivate func receive_award_handle(){
        
        receive_award_btn.isUserInteractionEnabled = false
        
        TIANSession(requestUrl: PH.receiveTimedRewardsApi,parameters: ["id":json["id"].stringValue]).withUnretained(self).subscribe{ own , result in
            
            /// 领取任务奖励后 随便发个通知便于 [我的]TAB栏刷新
            NotificationCenter.default.post(name: Notification.Name.nick_name_audit_failure, object: nil)
            
            own.receive_award_btn.isUserInteractionEnabled = true
            
            switch result {
            case .success:
                
                own.parentViewController?.goback_previous_controller(animated: false, completion: {
                    
                    SCComponets.alert_dispatcher?.nextAlert()
                })
                
            case .failure(let err):
                
                own.parentViewController?.toast(text: err.e)
                
                main_thread_execute(afterDelay: 1.5) {
                    
                    own.parentViewController?.goback_previous_controller(animated: false, completion: {
                        
                        SCComponets.alert_dispatcher?.nextAlert()
                    })
                    
                }
            }
            
        }.disposed(by: rx.disposeBag)
        
        
    }
    
    
}


//MARK: -

class ST_TimedRewardValueView: UIView {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        self.addSubview(textValueLabel)
        self.addSubview(unitTextLabel)
        
        textValueLabel.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
        }
        unitTextLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview().offset(8)
            make.left.equalTo(textValueLabel.snp.right).offset(4)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var textValueLabel:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.font = .systemFont(ofSize: 58) 
        return l
    }()
    
    
    fileprivate lazy var unitTextLabel:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.font = .systemFont(ofSize: 20, weight: .semibold)
        return l
    }()
    
    
}
